package com.sk.weichat.ui.me.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewAfterTextChangeEvent;
import com.sk.weichat.R;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.base.EditBaseActivity;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.ClearEditText;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import okhttp3.Call;


/**
 * 实名证件信息
 */
public class RealNameInfoActivity extends EditBaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    private TextView tv_title_center;
    private Button btn_sure;
    private ClearEditText edit_input_name;
    private ClearEditText edit_input_idcard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_name_authentication);
        getSupportActionBar().hide();
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void helloEventBus(String str) {
        if (str.equals("set_question")) {
            finish();
        }
    }

    private void initView() {
        ivBack = findViewById(R.id.iv_title_left);
        tv_title_center = findViewById(R.id.tv_title_center);
        tv_title_center.setText(getString(R.string.idcar_infor));
        edit_input_name = findViewById(R.id.edit_input_name);
        edit_input_idcard = findViewById(R.id.edit_input_idcard);
        ivBack.setOnClickListener(this);
        btn_sure = findViewById(R.id.btn_sure);
        btn_sure.setOnClickListener(this);

        RxTextView.afterTextChangeEvents(edit_input_name).mergeWith(RxTextView.afterTextChangeEvents(edit_input_idcard))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TextViewAfterTextChangeEvent>() {
                    @Override
                    public void accept(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) throws Exception {
                        if (!TextUtils.isEmpty(edit_input_name.getText())) {
                            btn_sure.setEnabled(true);
                            btn_sure.setBackgroundResource(R.drawable.shape_corner_26dp_solid_primary);
                        } else {
                            btn_sure.setEnabled(false);
                            btn_sure.setBackgroundResource(R.drawable.shape_corner_26_solid_gray_bdc2c8);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
        //弹出键盘输入
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        edit_input_name.setFocusable(true);
        edit_input_name.setFocusableInTouchMode(true);
        edit_input_name.requestFocus();
    }


    @Override
    public void onClick(View v) {
        if (v == ivBack) {
            finish();
        } else if (v == btn_sure) {
            if (TextUtils.isEmpty(edit_input_name.getText().toString())) {
                ToastUtil.showToast(this, getString(R.string.tips_input_Id_name));
            } else if (TextUtils.isEmpty(edit_input_idcard.getText().toString())) {
                ToastUtil.showToast(this, getString(R.string.tips_input_idcard));
            } else {
                RealNameAuto(edit_input_name.getText().toString(), edit_input_idcard.getText().toString());
            }
        }
    }

    /**
     * 验证用户绑定的身份证号和姓名
     */
    public void RealNameAuto(String name, String idCard) {
        if (!DialogHelper.isShowing()) {
            DialogHelper.showDefaulteMessageProgressDialog(this);
        }
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("name", name);
        params.put("idCard", idCard);
        HttpUtils.post().url(coreManager.getConfig().AIYUN_AUTH_INFO)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {
                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        DialogHelper.dismissProgressDialog();
                        if (result.getResultCode() == 1) {
                            startActivity(new Intent(mContext, SetIdentifyQuestionActivity.class));
                        } else {
                            ToastUtil.showToast(RealNameInfoActivity.this, result.getResultMsg());
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        DialogHelper.dismissProgressDialog();
                        ToastUtil.showToast(RealNameInfoActivity.this, e.toString());

                    }
                });
    }
}
